import os
from pathlib import Path

from pydantic_settings import BaseSettings


class ProjectSetting(BaseSettings):
    #项目所在目录,用于获取其它文件路径
    PROJECT_PATH: str = ''

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.PROJECT_PATH = Path(__file__).resolve().parents[2]

    @property
    def knowledge_files_path(self):
        _path=f'{self.PROJECT_PATH}{os.sep}knowledge_files'
        if not Path(_path).exists():
            print(f'PATH {_path} not exists')
        return _path

    def get_file_path(self,sub_path:str=None,file_name=None):
        """获取文件目录,PROJECT_PATH/sub_path/file_name
        如果子目录不存在则创建
        """
        _path=self.PROJECT_PATH
        if sub_path:
            _path = f'{_path}{os.sep}{sub_path}'
        if not Path(_path).exists():
            os.makedirs(_path)
        if file_name:
            return f'{_path}{os.sep}{file_name}'
        return _path

project_setting = ProjectSetting()